//导入request.js请求工具
import request from '@/utils/request'

export const userService = {
    // 获取待审核的教师列表
    getPendingTeachers() {
        return request.get('/user/pending-teachers')
    },

    // 处理教师审核
    handleTeacherApproval(data) {
        return request.post('/user/handle-teacher-approval', data)
    },

    // 获取指定角色的用户列表
    getUsersByRole(role) {
        return request.get(`/user/users-by-role/${role ?? 'all'}`)
    },

    // 更新用户状态
    updateUserStatus(data) {
        return request.patch('/user/update-status', data)
    },

    // 删除用户
    deleteUser(userId) {
        return request.delete(`/user/${userId}`)
    }
}

//提供调用注册接口的函数
export const userRegisterService = (registerData)=>{
    //借助于UrlSearchParams完成传递
    const params = new URLSearchParams()
    for(let key in registerData){
        params.append(key,registerData[key]);
    }
    return request.post('/user/register', JSON.stringify(registerData), {
        headers: {
            'Content-Type': 'application/json'
        }
    });
}

//提供调用登录接口的函数
export const userLoginService = (loginData)=>{
    const params = new URLSearchParams();
    for(let key in loginData){
        params.append(key,loginData[key])
    }
    return request.post('/user/login',params)
}

//获取用户详细信息
export const userInfoService = ()=>{
    return request.get('/user/userInfo')
}

//根据用户名获取用户信息
export const getUserInfoByUsernameService = (username)=>{
    return request.get(`/user/userInfo/${username}`)
}

//修改个人信息
export const userInfoUpdateService = (userInfoData)=>{
    return request.put('/user/update',userInfoData)
}

//修改头像
export const userAvatarUpdateService = (avatarUrl)=>{
    const params = new URLSearchParams();
    params.append('avatarUrl', avatarUrl)
    return request.patch('/user/updateAvatar',params)
}

// 发送验证码
export const sendVerificationCodeService = (data) => {
    return request.post('/user/sendVerificationCode', data, {
        headers: {
            'Content-Type': 'application/json' // 设置请求类型为application/json
        }
    });
}

// 重置密码
export const resetPasswordService = (data) => {
    return request.patch('/user/resetPassword', data, {
        headers: {
            'Content-Type': 'application/json'
        }
    });
}

// 通过邮箱验证码重置密码
export const forgotPasswordService = (data) => {
    return request.post('/user/forgotPassword', data, {
        headers: {
            'Content-Type': 'application/json'
        }
    });
}